from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template
import random

class MainHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'helloworld.html')
    self.response.out.write(template.render(path, template_values))

class QuizHandler(webapp.RequestHandler):
  def get(self):
    template_values = {'name': self.request.get('name')}
    path = os.path.join(os.path.dirname(__file__), 'quiz.html')
    self.response.out.write(template.render(path, template_values))

class WrongAnswer(object):
  def __init__(self, question, guess, correct):
    self.question=question
    self.guess= guess
    self.correct= correct

class GradeQuizHandler(webapp.RequestHandler):
  def get(self):
    addition_guess= self.request.get('addition')
    capital_guess= self.request.get('capital').strip().lower()
    template_values= {'name': self.request.get('name'), 'wrong_answers': []}
    #have to extract name again bc entirely separate from quizhandler

    if (addition_guess != '7'):
      template_values['wrong_answers'].append(WrongAnswer("3 + 4", addition_guess, 7))
    if (capital_guess == 'sacramento'):
       template_values['wrong_answers'].append(WrongAnswer("the capital of california", capital_guess, 'sacramento'))

    path = os.path.join(os.path.dirname(__file__), 'grade_quiz.html')
    self.response.out.write(template.render(path, template_values))

class GoodByeHandler (webapp.RequestHandler):
  def get(self):
    self.response.out.write('Goodbye app engine world!')

class DiceHandler (webapp.RequestHandler):
  def get(self):
    times= self.request.get('times')

    while not (times.isdigit()):
        self.response.out.write ('please type in another <i>time</i> ')
        self.request.get('times')
        

    self.response.out.write('times '+ times+ '<br>')

    # Number of times to roll the dice.
    DICE_ROLLS = 0
    doublesix = False

    NUMSIDES = 6

    def RollDice():
	    return random.randint(1, int(NUMSIDES))

    while doublesix == False:
	    # Simulate rolling two dice.
	    dice_1 = RollDice()
	    dice_2 = RollDice()
	    self.response.out.write ('Rolls: '+ str(dice_1) +' '+ str(dice_2)+ '<br>')

	    DICE_ROLLS= DICE_ROLLS +1 
	    if dice_1 == dice_2:

		    if dice_1 ==1:
			    self.response.out.write ("Got snake eyes!<br>")

		    if dice_1 == 6:
			    self.response.out.write("Got double sixes!<br>")
			    doublesix= True


    self.response.out.write("Rolling the dice " + str(DICE_ROLLS) + " times<br>")

def main():
  application = webapp.WSGIApplication([('/hello.*', MainHandler), ('/goodbye.*', GoodByeHandler), ('/dice.*', DiceHandler), ('/quiz.*', QuizHandler), ('/grade_quiz', GradeQuizHandler)], debug=True)

  run_wsgi_app(application)

if __name__ == '__main__':
  main()
